<template>
  <el-dialog :visible.sync="showDialog" :close-on-click-modal="false" width="80%" append-to-body :title="showTitle">

    <div class="page-content">
      <el-table :key="0" v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%">
        <el-table-column label="ID" align="center" width="100">
          <template slot-scope="{ row }">
            {{ row.mid_table_remark_007 }}
          </template>
        </el-table-column>
        <el-table-column label="查询条件" align="center">
          <template slot-scope="{ row }">

            <el-popover placement="left" width="200" trigger="click">
              <div class="popover-box">
                <div class="cont-item" v-html="row.show_mid_table_remark_001"></div>
              </div>
              <div class="table-cell-overflow" slot="reference">{{ row.show_mid_table_remark_001 | showBRContent }}</div>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="查询结果" align="center">
          <template slot-scope="{ row }">
            <el-popover placement="left" width="600" trigger="click">
              <div class="popover-box nowrap">
                <pre class="cont-item" v-html="row.show_mid_table_remark_002"></pre>
              </div>
              <div class="table-cell-overflow" slot="reference">{{ row.show_mid_table_remark_002 | showBRContent }}</div>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="查询时间" align="center" width="160" :resizable="false">
          <template slot-scope="{ row }">
            {{ row.time_insert }}
          </template>
        </el-table-column>
        <el-table-column label="评价等级" align="center" width="110" :resizable="false">
          <template slot-scope="{ row }">
            <!-- {{ row.mid_table_remark_018 }} -->
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="100" class-name="small-padding fixed-width" :resizable="false">
          <template slot-scope="{ row }">
            <el-button class="table-btn col-danger" type="text" size="mini" @click="handleDel(row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">关闭</el-button>
      <el-button type="primary" @click="handleRecordClassic"
        :disabled="disabledRecord.disabled">{{ disabledRecord.text }}</el-button>
    </div>
    <comp-add-to-classic ref="refAddToClassic" @refresh="onRefresh"></comp-add-to-classic>
  </el-dialog>
</template>
<script>
import { Base64 } from "js-base64";
import { middleList } from "@/api/middle";
import Pagination from "@/components/Pagination";
import CompAddToClassic from "./add_classic.vue"
export default {
  name: "CompChatDetail",
  components: {
    Pagination,
    CompAddToClassic
  },
  data() {
    return {
      showDialog: false,
      list: [],
      listLoading: false,
      rowInfo: null,
      industryInfo: null
    }
  },
  computed: {
    showTitle() {
      let title = this.rowInfo?.mid_table_remark_013 || "";
      return title
    },
    disabledRecord() {
      let disabled = !!(this.rowInfo?.tmp_recorded_status || false)
      return { disabled, text: disabled ? '已收录' : '收录为经典' };
    }
  },
  filters: {
    showBRContent(val) {
      if (!val) { return val }
      return val.replace(/<br\s*\/?>/g, "\n")
    }
  },

  methods: {
    async getList() {
      let queryData = {
        invoke_method: "public_load_middle_table_list",
        pre_db_para_middle_table_name: "public_middle_table_lite_mod_ai_chatgpt",
        type_middle_app: 10001,
        type_inner_app: 1001,
        is_online: 0,
        pre_db_para_begin_idx: 0,
        pre_db_para_length_4_list: 10,
        mid_table_remark_013_in: this.rowInfo.mid_table_remark_013,
        pre_db_para_order_by_para: "time_update desc"
      }
      return middleList(this.Qs.stringify(queryData));
    },
    async onShowDialog(row, industryInfo) {

      if (!row || !row.mid_table_remark_013 || !industryInfo) {
        return;
      }
      this.rowInfo = row;
      this.industryInfo = industryInfo
      let response = await this.getList().catch((err) => {
        console.log("err>>>>", err)
      });
      if (response) {
        let result = response.page_content || [];
        for (let i = 0; i < result.length; i++) {
          let item = result[i];
          item.show_mid_table_remark_001 = item.mid_table_remark_001
          try {
            if (item.mid_table_remark_001) {
              item.show_mid_table_remark_001 = Base64.decode(item.show_mid_table_remark_001)
            }
            if (item.mid_table_remark_002) {
              item.show_mid_table_remark_002 = Base64.decode(item.mid_table_remark_002)
            }
          } catch (error) {
          }
        }
        this.list = response.page_content;
        this.showDialog = true;
      }
    },
    handleClose() {
      this.showDialog = false
    },
    async handleRecordClassic() {
      let row = Object.assign({}, this.industryInfo, { pre_cmd_para_session_id: this.rowInfo.mid_table_remark_013, pre_cmd_para_classify_tag: "" });
      this.$refs.refAddToClassic.onEditOrAdd(row)
      return;
    },
    handleDel(row) {


      this.$confirm("您确定要删除该结果吗?", "提示", {
        type: "warning",
      })
        .then(() => {
          middleList(
            this.Qs.stringify({
              invoke_method: "public_insert_or_update_middle_table",
              pre_db_para_middle_table_name: "public_middle_table_lite_mod_ai_chatgpt",
              pre_db_para_id_public_middle_table: row.id_public_middle_table,
              type_middle_app: 10001,
              type_inner_app: 1001,
              is_online: 111

            })
          )
            .then((response) => {
              this.$notify({
                title: "Success",
                message: "删除成功",
                type: "success",
                duration: 2000,
              });
              this.$emit('refresh')
              this.showDialog=false;

            })
            .catch(() => { });
        })
        .catch(() => { });


    },
    onRefresh(){
      this.$emit('refresh')
      this.showDialog=false;
    }
  }
}
</script>
<style lang="scss">
@import "../../../styles/common.scss";
</style>
